﻿namespace Dcms.DAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;

    public class Dcms_ImagesDAL : IDcms_ImagesDAL
    {
        public Dcms_ImagesModel AddModel(Dcms_ImagesModel model)
        {
            string sQLString = "insert into Dcms_Images(Img_Name,Img_Path,Img_Type,Img_Date) values(@Img_Name,@Img_Path,@Img_Type,@Img_Date) SELECT @NewId=@@Identity";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@NewId", SqlDbType.Int), new SqlParameter("@Img_Name", SqlDbType.VarChar), new SqlParameter("@Img_Path", SqlDbType.VarChar), new SqlParameter("@Img_Type", SqlDbType.Int), new SqlParameter("@Img_Date", SqlDbType.DateTime) };
            cmdParms[0].Direction = ParameterDirection.Output;
            cmdParms[1].Value = model.Img_Name;
            cmdParms[2].Value = model.Img_Path;
            cmdParms[3].Value = model.Img_Type;
            cmdParms[4].Value = model.Img_Date;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            model.Img_ID = (int) cmdParms[0].Value;
            return model;
        }

        public IList<Dcms_ImagesModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_ImagesModel> list = new List<Dcms_ImagesModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_ImagesModel item = new Dcms_ImagesModel();
                if (ds.Tables[0].Rows[i]["Img_ID"].ToString() != "") item.Img_ID = int.Parse(ds.Tables[0].Rows[i]["Img_ID"].ToString());
                item.Img_Name = ds.Tables[0].Rows[i]["Img_Name"].ToString();
                item.Img_Path = ds.Tables[0].Rows[i]["Img_Path"].ToString();
                if (ds.Tables[0].Rows[i]["Img_Type"].ToString() != "") item.Img_Type = int.Parse(ds.Tables[0].Rows[i]["Img_Type"].ToString());
                if (ds.Tables[0].Rows[i]["Img_Date"].ToString() != "") item.Img_Date = DateTime.Parse(ds.Tables[0].Rows[i]["Img_Date"].ToString());
                list.Add(item);
            }
            return list;
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_Images where Img_ID=@Img_ID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Img_ID", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
        }

        public Dcms_ImagesModel GetModelByID(int ID)
        {
            string sQLString = "select * from Dcms_Images where Img_ID=@Img_ID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Img_ID", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            Dcms_ImagesModel model = new Dcms_ImagesModel();
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_ImagesModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public DataSet GetModelList()
        {
            string sql = "select * from Dcms_Images";
            return SqlHelper.CreateDataSet(sql);
        }

        public Dcms_ImagesModel Update(Dcms_ImagesModel model)
        {
            string sQLString = "update Dcms_Images set Img_Name=@Img_Name,Img_Path=@Img_Path,Img_Type=@Img_Type,Img_Date=@Img_Date where Img_ID=@Img_ID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Img_Name", SqlDbType.VarChar), new SqlParameter("@Img_Path", SqlDbType.VarChar), new SqlParameter("@Img_Type", SqlDbType.Int), new SqlParameter("@Img_Date", SqlDbType.DateTime), new SqlParameter("@Img_ID", SqlDbType.Int) };
            cmdParms[0].Value = model.Img_Name;
            cmdParms[1].Value = model.Img_Path;
            cmdParms[2].Value = model.Img_Type;
            cmdParms[3].Value = model.Img_Date;
            cmdParms[4].Value = model.Img_ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }
    }
}

